﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RoleList.aspx.cs" Inherits="Role_RoleList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>角色管理</title>
    <link href="../js/jquery-easyui-1.2.6/themes/default/easyui.css" rel="stylesheet"
        type="text/css" />
    <script src="../js/jquery-easyui-1.2.6/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="../js/jquery-easyui-1.2.6/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../js/jquery-easyui-1.2.6/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <link href="../css/wms.css" rel="stylesheet" type="text/css" />
    <link href="../css/icon.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        var grd;
        var dlgEdit;
        var dlgEditForm;
        var dlgEditRolePermission;

        $(function () {
            grd = $('#rolelist').datagrid({
                nowrap: true,
                striped: true,
                remoteSort: false,
                fitColumns: true,
                url: "Role.ashx?action=list",
                frozenColumns: [[
                    { field: 'ck', checkbox: true }
                ]],
                columns: [[
                    { field: 'RoleName', title: '角色名称', width: 100 },
                    { field: 'RoleDescr', title: '角色描述', width: 100 },
                    { field: 'Permission', title: '权限', width: 80, align: 'center',
                        formatter: function (value, p, index) {
                            return '<a onclick="EditPermission(\'' + p.Id + '\');" class="easyui-linkbutton l-btn l-btn-plain" iconcls="icon-permission" plain="true"><span class="l-btn-left"><span class="l-btn-text icon-permission" style="padding-left: 20px;">设置权限</span></span></a>';
                        }
                    },
                    { field: 'WareHouse', title: '角色仓库', width: 80, align: 'center',
                        formatter: function (value, p, index) {
                            return '<a onclick="EditWareHouse(\'' + p.Id + '\');" class="easyui-linkbutton l-btn l-btn-plain" iconcls="icon-permission" plain="true"><span class="l-btn-left"><span class="l-btn-text icon-permission" style="padding-left: 20px;">角色仓库</span></span></a>';
                        }
                    },
                    { field: 'Operate', title: '操作', width: 70, align: 'center',
                        formatter: function (value, u, index) {
                            var e = '<a onclick="Edit(\'' + u.Id + '\');" title="编辑" iconcls="icon-edit-blue" class="easyui-linkbutton l-btn l-btn-plain" plain="true"><span class="l-btn-left"><span class="l-btn-text"><span class="l-btn-empty icon-edit-blue">&nbsp;</span></span></span></a>';
                            var d = '<a onclick="Delete(\'' + u.Id + '\')" title="删除" iconcls="icon-delete" class="easyui-linkbutton l-btn l-btn-plain" plain="true"><span class="l-btn-left"><span class="l-btn-text"><span class="l-btn-empty icon-delete">&nbsp;</span></span></span></a>';
                            return e + d;
                        }
                    }
                ]],
                pagination: true,
                rownumbers: true,
                toolbar:
                [
                    {
                        id: 'btnAdd',
                        text: '添加',
                        iconCls: 'icon-add-blue',
                        handler: Add
                    },
                    '-',
                    {
                        id: 'btnDelete',
                        text: '批量删除',
                        iconCls: 'icon-delete',
                        handler: function () {
                            var rows = $("#rolelist").datagrid("getSelections");

                            if (rows.length == 0) {
                                $.messager.show({
                                    title: '操作提示',
                                    msg: '请选择您要删除的记录！',
                                    showType: 'slide',
                                    timeout: 3000
                                });
                            }
                            else {
                                $.messager.confirm("操作提示", "您确定要删除选中的记录吗？", function (data) {
                                    if (data) {
                                        var idlist = [];

                                        $.each(rows, function (i, row) {
                                            idlist.push(row.Id);
                                        });

                                        $.ajax({
                                            type: "POST",
                                            url: "Role.ashx",
                                            data: "action=delete&Id=" + idlist.join(','),
                                            success: function (data) {
                                                eval('data=' + data);
                                                if (data.success) {
                                                    $.messager.alert('操作提示', '删除成功！', "info", function () {
                                                        $('#rolelist').datagrid("reload");
                                                    });
                                                }
                                                else {
                                                    $.messager.alert('数据操作异常', data.message, "error");
                                                }
                                            },
                                            error: function () {
                                                $.messager.alert('异步操作异常', '异步操作错误！', "error");
                                            }
                                        });
                                    }
                                });
                            }
                        }
                    }
                ]
            });

            dlgEdit = $("#dlgEditRole").dialog({
                closed: true,
                width: 500,
                height: 350,
                closable: true,
                draggable: true,
                resizable: false,
                modal: true,
                buttons:
                [
                    {
                        text: '保存',
                        iconCls: 'icon-ok',
                        handler: Save
                    },
				    {
				        text: '取消',
				        iconCls: 'icon-cancel',
				        handler: function () {
				            dlgEdit.dialog('close');
				        }
				    }
				],
                onClose: function () {
                    grd.datagrid('clearSelections');
                }
            });

            dlgEditForm = dlgEdit.find('form');

            dlgEditRolePermission = $("#dlgEditRolePermission").dialog({
                closed: true,
                width: 600,
                height: 450,
                closable: true,
                draggable: true,
                resizable: false,
                modal: true,
                buttons:
                [
                    {
                        text: '保存',
                        iconCls: 'icon-ok',
                        handler: function () {
                            var roleIDList = new Array();
                            $("#rolePermission :checkbox").each(function () {
                                if ($(this).is(":checked")) {
                                    roleIDList.push($(this).attr("id"));
                                }
                            });
                            $.ajax({
                                type: "POST",
                                url: "Role.ashx",
                                data: "action=savepermission&ID=" + $("#hdRoleID").val() + "&Rights=" + roleIDList.join(','),
                                success: function (oData) {
                                    eval('oData=' + oData);
                                    if (oData.success) {
                                        $.messager.alert('操作提示', '保存成功！', "info", function () {
                                            dlgEditRolePermission.dialog('close');
                                        });
                                    }
                                    else {
                                        $.messager.alert('数据操作异常', oData.message, "error");
                                    }
                                },
                                error: function () {
                                    $.messager.alert('异步操作异常', '异步操作错误！', "error");
                                }
                            });
                        }
                    },
				    {
				        text: '取消',
				        iconCls: 'icon-cancel',
				        handler: function () {
				            dlgEditRolePermission.dialog('close');
				        }
				    }
				],
                onClose: function () {
                    grd.datagrid('clearSelections');
                }
            });

            
        });

        // 添加
        function Add() {
            $("#RoleName").removeAttr("readonly").removeClass("readonly");
            dlgEdit.dialog('open');
            dlgEdit.form('clear');
            $("#RoleName").focus();
            dlgEditForm.url = 'Role.ashx?action=insert';
        }

        // 编辑
        function Edit(id) {
            // 编辑时VIP卡号不可修改
            $("#RoleName").attr("readonly", "readonly").addClass("readonly");
            dlgEdit.dialog('open');
            dlgEditForm.form('load', 'Role.ashx?action=read&ID=' + id);
            dlgEditForm.url = 'Role.ashx?action=update&ID=' + id;
        }

        // 删除
        function Delete(id) {
            $.messager.confirm("操作提示", "您确定要删除选中的记录吗？", function (data) {
                if (data) {
                    $.ajax({
                        type: "POST",
                        url: "Role.ashx",
                        data: "action=delete&Id=" + id,
                        success: function (data) {
                            eval('data=' + data);
                            if (data.success) {
                                $.messager.alert('操作提示', '删除成功！', "info", function () {
                                    grd.datagrid("reload");
                                });
                            }
                            else {
                                $.messager.alert('数据操作异常', data.message, "error");
                            }
                        },
                        error: function () {
                            $.messager.alert('异步操作异常', '异步操作错误！', "error");
                        }
                    });
                }
                else {
                    grd.datagrid('clearSelections');
                }
            });
        }

        // 保存
        function Save() {
            if ($("#RoleName").val() == "") {
                $.messager.alert("操作提示", "角色名称不能为空", "error");
                return;
            }
            if ($.browser.msie && ($.browser.version == "6.0") && !$.support.style) {
                if (dlgEditForm.url == "Role.ashx?action=insert") {
                    $.ajax({
                        type: "POST",
                        url: "Role.ashx",
                        data: "action=exists&RoleName=" + encodeURI($("#RoleName").val()),
                        success: function (oData) {
                            eval('oData=' + oData);
                            if (oData.success) {
                                $.messager.alert('操作提示', oData.message, "error");
                            }
                            else {
                                dlgEditForm.form('submit', {
                                    url: dlgEditForm.url,
                                    onSubmit: function () {
                                        return $(this).form('validate');
                                    },
                                    success: function (data) {
                                        dlgEdit.dialog('close');
                                        $.messager.alert('操作提示', '保存成功！', "info", function () {
                                            grd.datagrid("reload");
                                        });
                                    }
                                });
                            }
                        },
                        error: function () {
                            $.messager.alert('异步操作异常', '异步操作错误！', "error");
                        }
                    });
                } else {
                    dlgEditForm.form('submit', {
                        url: dlgEditForm.url,
                        onSubmit: function () {
                            return $(this).form('validate');
                        },
                        success: function (data) {
                            dlgEdit.dialog('close');
                            $.messager.alert('操作提示', '保存成功！', "info", function () {
                                grd.datagrid("reload");
                            });
                        }
                    });
                }
            } else {
                dlgEditForm.form('submit', {
                    url: dlgEditForm.url,
                    onSubmit: function () {
                        return $(this).form('validate');
                    },
                    success: function (data) {
                        eval('data=' + data);
                        if (data.success) {
                            $.messager.alert('操作提示', '保存成功！', "info", function () {
                                dlgEdit.dialog('close');
                                grd.datagrid("reload");
                            });
                        }
                        else {
                            $.messager.alert('操作提示', data.message, "error");
                        }
                    }
                });
            }
        }

        // 查询
        function Search() {
            grd.datagrid('options').url = 'Role.ashx?action=list&RoleName=' + encodeURI($("#txtRoleName").val());
            grd.datagrid("reload");
        }

        // 清空
        function Reset() {
            $("#txtRoleName").val("");
            grd.datagrid('options').url = 'Role.ashx?action=list';
            grd.datagrid("reload");
        }

        // 编辑
        function EditPermission(id) {
            $.ajax({
                type: "get",
                dataType: "json",
                url: "Role.ashx?action=rolepermission&ID=" + id,
                beforeSend: function () {
                    $("#rolePermission").empty();
                },
                success: function (data) {
                    $("#txtPermissionRoleName").val(data.RoleName);
                    $("#hdRoleID").val(data.RoleID);
                    $("#rolePermission").html(data.message);
                    $("#rolePermission").tree();
                    $("#rolePermission :checkbox").each(function () {
                        if (data.Rights.indexOf($(this).attr("id")) != -1) {
                            $(this).attr("checked", "checked");
                        } else {
                            $(this).removeAttr("checked");
                        }
                    });
                }
            });
            dlgEditRolePermission.dialog('open');
        }

        function EditWareHouse(id) {
            dlgEditWareHouse = $("#dlgEditWareHouse").dialog({
                closed: true,
                width: 600,
                height: 450,
                closable: true,
                draggable: true,
                resizable: false,
                modal: true,
                href: "../Stock/WhsRoles.aspx?id=" + id + "&r=" + Math.random(),
                onClose: function () {
                    grd.datagrid('clearSelections');
                }
            });
            dlgEditWareHouse.dialog('open');
        }
    </script>
    <script type="text/javascript">
        $(function () {
            // 查询条件
            $("#search").bind("click", function () {
                Search();
            });
            // 清空
            $("#reset").bind("click", function () {
                Reset();
            });

            // 回车键
            $("#RoleName").bind("keydown", function (e) {
                var e = window.event || e;
                var element = e.srcElement || e.target;
                if (e.keyCode == 13) {
                    $("#RoleDescr").focus();
                }
            });
        });
    </script>
    <style type="text/css">
        .search-condition input
        {
            width: 100px;
        }
    </style>
</head>
<body>
    <div class="easyui-panel" title="查询条件" iconcls="icon-search" collapsible="true">
        <div class="search-condition">
            角色名称：<input id="txtRoleName" type="text" />
            <a id="search" class="easyui-linkbutton" plain="true" iconcls="icon-search">查询</a>
            <a id="reset" class="easyui-linkbutton" plain="true" iconcls="icon-empty">清空</a>
        </div>
    </div>
    <div id="rolelist" style="margin-top: 5px; clear: both;">
    </div>
    <div id="dlgEditRole" title="编辑角色" iconcls="icon-add-blue">
        <form id="frmRole" method="post">
        <table class="table-dialog">
            <tr>
                <td>
                    角色名称：
                </td>
                <td>
                    <input id="RoleName" name="RoleName" type="text" />
                    <span class="required">*</span>
                </td>
            </tr>
            <tr>
                <td>
                    角色描述：
                </td>
                <td>
                    <textarea id="RoleDescr" name="RoleDescr" rows="4" cols="40"></textarea>
                </td>
            </tr>
        </table>
        </form>
    </div>
    <div id="dlgEditRolePermission" title="编辑角色权限">
        <table style="width: 100%;">
            <tr>
                <td style="width: 100px; padding-right:20px; text-align: right;">
                    角色名称：
                </td>
                <td><input id="hdRoleID" type="hidden" />
                    <input id="txtPermissionRoleName" type="text" class="readonly" style="border: 1px solid #D2D2D2; line-height: 22px; height:22px; width: 120px;" readonly="readonly" />
                </td>
            </tr>
            <tr>
                <td style="text-align: right; padding-right:20px;">
                    角色权限：
                </td>
                <td>
                    <ul id="rolePermission">
                    </ul>
                </td>
            </tr>
        </table>
    </div>
    <div id="dlgEditWareHouse" title="编辑角色仓库">
    </div>
</body>
</html>
